local liucang = fk.CreateSkill{
  name = "plum__liucang",
}

Fk:loadTranslationTable{
  ["plum__liucang"] = "流藏",
  [":plum__liucang"] = "你受到伤害时，可以展示并交给一名其他角色任意张牌，令此伤害改为冰属性并转移给其。若交出牌均因此被弃置，你失去1点体力。",

  ["#plum__liucang-choose"] = "流藏：可以可以展示并交给一名其他角色任意张牌，令此伤害改为冰属性并转移给其",

  ["@@plum__liucang-inhand"] = "流藏",

  ["$plum__liucang1"] = "为国报耻，然后伏死，足以无恨。",
  ["$plum__liucang2"] =  "万里流离，死生共之。",
}

---@param room Room
---@param player ServerPlayer @ 接受者
---@param cards integer[] @ 给出牌
---@param start_id integer @ 起始事件的id
---@return boolean @ “流藏”是否失败
local liucangDefeat = function(room, player, cards, start_id)
  for i = start_id, #room.logic.all_game_events do
    local e = room.logic.all_game_events[i]
    if e.event == GameEvent.MoveCards then
      for _, move in ipairs(e.data) do
        if move.from == player and move.skillName == "ice_damage_skill" then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(cards, info.cardId) then
              table.removeOne(cards, info.cardId)
              if #cards == 0 then
                return true
              end
            end
          end
        end
      end
    end
  end
  return false
end

liucang:addEffect(fk.DamageInflicted, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(liucang.name) and target == player and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos, cards, ret = room:askToChooseCardsAndPlayers(player, {
      min_card_num = 1,
      max_card_num = 999,
      targets = room:getOtherPlayers(player, false),
      min_num = 1,
      max_num = 1,
      skill_name = liucang.name,
      cancelable = true,
      prompt = "#plum__liucang-choose"
    })
    if ret and #tos == 1 and #cards > 0 then
      event:setCostData(self, {tos[1], cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local to, cards = table.unpack(event:getCostData(self)) ---@type ServerPlayer, integer[]
    local room = player.room
    local n = data.damage
    data:preventDamage()
    room:showCards(cards, player)
    room:obtainCard(to, cards, true, fk.ReasonGive, player, liucang.name, "@@plum__liucang-inhand")
    if to:isAlive() then
      room:damage{
        from = data.from,
        to = to,
        damage = n,
        damageType = fk.IceDamage,
        skillName = data.skillName,
        chain = data.chain,
        card = data.card,
      }
      table.forEach(cards, function (id)
        room:removeCardMark(Fk:getCardById(id), "@@plum__liucang-inhand")
      end)
      if player:isAlive() and liucangDefeat(room, to, cards, room.logic:getCurrentEvent().id) then
        room:loseHp(player, 1, liucang.name)
      end
    end
  end
})

return liucang